package com.yair_srm.integration.sap.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import com.yair_srm.integration.sap.IPOList_SAP;
import com.yair_srm.integration.sap.ISAP;
import com.yair_srm.model.bean.PurchaseOrder;

public class POList_SAP implements IPOList_SAP{
	
    private SAP_AS_POOL sapconnector = SAP_AS_POOL.getInstance();
    private String functionname;
    private JCoFunction function;
    
    public POList_SAP(){
    	sapconnector = SAP_AS_POOL.getInstance();
    	functionname = "ZSRM_GET_POLIST";
    	try {
			function = sapconnector.fetchfunction(functionname);
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
	public ArrayList<PurchaseOrder> getPOList(String vendor,HashMap parameter) {
		System.out.println("in get polist");
		
		String startdt = (String) parameter.get("startdt");
		String enddt = (String) parameter.get("enddt");
		String start = (String) parameter.get("start");
		String limit = (String) parameter.get("limit");
		
		String total = null;
		JCoTable polist_sap = null;
		ArrayList<PurchaseOrder> polist;
		
		try {
			//System.out.println(function.getName());
			JCoParameterList import_p = function.getImportParameterList();
			
			import_p.setValue("VENDOR", vendor);
			import_p.setValue("STARTDT", startdt);
			import_p.setValue("ENDDT", enddt);
			import_p.setValue("START", start);
			import_p.setValue("LIMIT", limit);
			function.execute(SAP_AS_POOL.getDestination());
			
			polist_sap = function.getTableParameterList().getTable("POLIST");
			total = function.getExportParameterList().getString("TOTAL");
			polist = new ArrayList();
			polist.clear();
			do{
				PurchaseOrder po = new PurchaseOrder();
				po.setOrder_id(polist_sap.getString("PONUMBER"));
				po.setOrder_date(polist_sap.getString("CREATIONDT"));
				polist.add(po);
				
			}while(polist_sap.nextRow());
			
			parameter.put("total", total);
			
			return polist;
			
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static void main(String[] args){
		System.out.println("start jco connect");
		POList_SAP sap = new POList_SAP();
		//sap.getPOList(null, null,"0000005550");
	}


}
